package com.gotop.auth.granter;//package com.gotop.api.granter;
//
//import com.gotop.api.service.user.IUserService;
//import com.gotop.common.exception.SecureException;
//import com.gotop.common.utils.StringUtils;
//import com.gotop.common.utils.WebUtil;
//import com.gotop.common.utils.auth.TokenParameter;
//import com.gotop.common.utils.auth.TokenUtil;
//import com.gotop.common.utils.auth.UserInfo;
//import com.gotop.entity.user.UserOauth;
//import lombok.AllArgsConstructor;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Objects;
//
///**
// * @program: cem
// * @description:
// * @author: superfish
// * @create: 2020-12-17 16:08
// **/
//@Component
//@AllArgsConstructor
//public class SocialTokenGranter implements ITokenGranter {
//
//    public static final String GRANT_TYPE = "social";
//
//    private static final Integer AUTH_SUCCESS_CODE = 2000;
//
//    private final IUserService userService;
//    private final SocialProperties socialProperties;
//
//    @Override
//    public UserInfo grant(TokenParameter tokenParameter) {
//        HttpServletRequest request = WebUtil.getRequest();
//        String tenantId = StringUtils.toStr(request.getHeader(TokenUtil.TENANT_HEADER_KEY), TokenUtil.DEFAULT_TENANT_ID);
//        // 开放平台来源
//        String sourceParameter = request.getParameter("source");
//        // 匹配是否有别名定义
//        String source = socialProperties.getAlias().getOrDefault(sourceParameter, sourceParameter);
//        // 开放平台授权码
//        String code = request.getParameter("code");
//        // 开放平台状态吗
//        String state = request.getParameter("state");
//
//        // 获取开放平台授权数据
//        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
//        AuthCallback authCallback = new AuthCallback();
//        authCallback.setCode(code);
//        authCallback.setState(state);
//        AuthResponse authResponse = authRequest.login(authCallback);
//        AuthUser authUser;
//        if (authResponse.getCode() == AUTH_SUCCESS_CODE) {
//            authUser = (AuthUser) authResponse.getData();
//        } else {
//            throw new SecureException("social grant failure, auth response is not success");
//        }
//
//        // 组装数据
//        UserOauth userOauth = Objects.requireNonNull(BeanUtil.copy(authUser, UserOauth.class));
//        userOauth.setSource(authUser.getSource());
//        userOauth.setTenantId(tenantId);
//        userOauth.setUuid(authUser.getUuid());
//        // 返回UserInfo
//        return userService.userInfo(userOauth);
//    }
//
//}